﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VISS.API.Users;
using NCT.Framework;
using System.Collections.Generic;

namespace VISS.Web.Frontend.Modules.User.Profile {

    public partial class ConversationPage : System.Web.UI.Page {

        protected void Page_Load(object sender, EventArgs e) {
            UserContext userContext = LoginFactory.GetCurrentUser();
            long conversationID = ConvertUtils.ToLong(Request.QueryString["conversationid"]);

            if (userContext == null) {
                Response.Redirect("/login?url=" + AppSettings.RootURL + "profile/conversation?cid=" + conversationID);
                return;
            }

            IUserMessage _objectMessage = StructureMapFactory.GetInstance<IUserMessage>();
            List<UserMessageEnt> msgList = _objectMessage.GetConversationMessages(userContext.UserKey, conversationID);

            if (msgList.Count > 0) {
                if (msgList[0].SentUserKey == userContext.UserKey) {
                    UserGeneralProfileEnt generalProfileEnt = StructureMapFactory.GetInstance<IUserGeneralProfile>().GetByUserKey(msgList[0].ReceivedUserKey);
                    idConversationDetail.OtherUserName = generalProfileEnt.Name;
                    idConversationDetail.OtherUserGender = generalProfileEnt.Gender;
                    idConversationDetail.OtherUserKey = msgList[0].ReceivedUserKey;
                }
                else {
                    idConversationDetail.OtherUserName = msgList[0].SenderName;
                    idConversationDetail.OtherUserKey = msgList[0].SentUserKey;
                }

                //Set Read for Conversation
                _objectMessage.SetRead(userContext.UserKey, conversationID);
            }
            idConversationDetail.ConversationMessages = msgList;
            idConversationDetail.CurrentUserKey = userContext.UserKey;
            if ((userContext.Status & EUserStatus.Verified) == EUserStatus.Verified) {
                idConversationDetail.CanDoAction = true;
            }
            else {
                idConversationDetail.CanDoAction = false;
            }

            // SEO
            IHeadSeo seo = this.Master as IHeadSeo;
            seo.Title = StringResources.SEO.TitleUserConversation + " | " + userContext.FullName + " | " + StringResources.SEO.TitleDefault;
            seo.Description = StringResources.SEO.DescriptionDefault;
            seo.Keywords = StringResources.SEO.KeywordsDefault;
        }
    }
}